import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(
    const MaterialApp(
      home: FlutterHeroAnimationFirstPage(),
    ),
  );
}
class FlutterHeroAnimationFirstPage extends StatelessWidget {
  const FlutterHeroAnimationFirstPage({super.key});

  @override
  Widget build(BuildContext context) {


    return Scaffold(
      body: Container(
        color: Colors.green,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Hero(
                tag: "HeroAnimationTag",
                child: Container(
                  width: 100,
                  height: 100,
                  margin: EdgeInsets.only(bottom: 10),
                  color: Colors.redAccent,
                ),
              ),
              GestureDetector(
                onTap: (){
                  _startHeroAnimation(context);
                  },
                child: Text(
                  "点击执行Hero动画",
                  style: TextStyle(color: Colors.black38),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
  void _startHeroAnimation(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (context){
      return FlutterHeroAnimationSecondPage();
    }));
    return;
    Navigator.push(context, PageRouteBuilder(
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation secondaryAnimation) {
          return new FadeTransition(
            opacity: animation,
            child: FlutterHeroAnimationSecondPage(),
          );
        })
    );
  }
}

class FlutterHeroAnimationSecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.blue,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Hero(
                tag: "HeroAnimationTag",
                child: Container(
                  height: 100,
                  margin: EdgeInsets.only(bottom: 10, left: 0, right: 0),
                  color: Colors.redAccent,
                ),
              ),
              GestureDetector(
                onTap: () {Navigator.pop(context);},
                child: Text(
                  "点击返回",
                  style: TextStyle(color: Colors.black38),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
